﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Hutech.CRM.Library.Models.Entities;
using Hutech.CRM.Library.Models.DataAccessLayer;
using Hutech.CRM.Library.Models.BusinessAccessLayer.BusinessObjects;
using Hutech.CRM.Library.Utilities;
using nvn.Library.Patterns;

namespace Hutech.CRM.Library.Models.BusinessAccessLayer
{
    class CriterionCustomerBAL : ICriterionCustomerBAL
    {
        #region Variable
        List<CriterionCustomer> ListResult = new List<CriterionCustomer>();
        string Symbol = "|";
        #endregion

        #region Private Method
        /// <summary>
        /// Persists a new instance of CriterionForGroupCustomer. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create(CriterionCustomer item)
        {
            item.CriterionGuid = Guid.NewGuid();

            int rowsAffected = CriterionCustomerDAL.Create(item);

            return (rowsAffected > 0);

        }

        /// <summary>
        /// Updates this instance of CriterionForGroupCustomer. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update(CriterionCustomer item)
        {
            return CriterionCustomerDAL.Update(item);
        }
        private CriterionCustomer PopulateFromReader(IDataReader reader)
        {
            CriterionCustomer item = new CriterionCustomer();
            if (reader.Read())
            {
                item.CriterionGuid = new Guid(reader[ConstantVariable.Column_CriterionGuid].ToString());
                item.CriterionName = reader[ConstantVariable.Column_CriterionName].ToString();
                item.GroupLevelCustomerGuid = new Guid(reader[ConstantVariable.Column_GroupLevelCustomerGuid].ToString());
                item.GroupLevelCustomerName = reader[ConstantVariable.Column_GroupLevelCustomerName].ToString();
                item.Priority = Convert.ToInt32(reader[ConstantVariable.Column_Priority]);
                item.IsActive = Convert.ToBoolean(reader[ConstantVariable.Column_IsActive]);
                item.LastUpdatedDate = Convert.ToDateTime(reader[ConstantVariable.Column_LastUpdatedDate]);
                item.URLIcon = reader[ConstantVariable.Column_URLIcon].ToString();
            }
            return item;
        }
        private List<CriterionCustomer> LoadListFromReader(IDataReader reader)
        {
            List<CriterionCustomer> criterionForGroupCustomerList = new List<CriterionCustomer>();
            try
            {
                while (reader.Read())
                {
                    CriterionCustomer criterionForGroupCustomer = new CriterionCustomer();
                    criterionForGroupCustomer.CriterionGuid = new Guid(reader[ConstantVariable.Column_CriterionGuid].ToString());
                    criterionForGroupCustomer.CriterionName = reader[ConstantVariable.Column_CriterionName].ToString();
                    criterionForGroupCustomer.GroupLevelCustomerGuid = new Guid(reader[ConstantVariable.Column_GroupLevelCustomerGuid].ToString());
                    criterionForGroupCustomer.GroupLevelCustomerName = reader[ConstantVariable.Column_GroupLevelCustomerName].ToString();
                    criterionForGroupCustomer.Priority = Convert.ToInt32(reader[ConstantVariable.Column_Priority]);
                    criterionForGroupCustomer.IsActive = Convert.ToBoolean(reader[ConstantVariable.Column_IsActive]);
                    criterionForGroupCustomer.LastUpdatedDate = Convert.ToDateTime(reader[ConstantVariable.Column_LastUpdatedDate]);
                    criterionForGroupCustomer.URLIcon = reader[ConstantVariable.Column_URLIcon].ToString();
                    criterionForGroupCustomerList.Add(criterionForGroupCustomer);
                }
            }
            finally
            {
                reader.Close();
            }
            return criterionForGroupCustomerList;
        }
        private void LoadDataForGroup(Guid GroupCustomerGuid)
        {
            IDataReader reader = CriterionCustomerDAL.GetAllGroup(GroupCustomerGuid);
            List<CriterionCustomer> Groups = LoadListFromReader(reader);
            
            if (Groups.Count == 0)
                return;
            else
            {
                foreach (CriterionCustomer itemGroup in Groups)
                {
                    CriterionCustomer item = itemGroup;
                    if (item.CriterionGuid != ConstantVariable.VALUE_EMTY_GUID)
                        Symbol += ConstantVariable.VALUE_MENU_SYMBOL;
                    item.Symbols = Symbol + " ";
                    ListResult.Add(item);
                    LoadDataForGroup(item.CriterionGuid);
                    if (item.CriterionGuid != ConstantVariable.VALUE_EMTY_GUID)
                        Symbol = Symbol.Remove(Symbol.Length - 6, 6);
                }
            }
        }
        #endregion

        #region Publish method
        /// <summary>
        /// Saves this instance of CriterionForGroupCustomer. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save(CriterionCustomer item)
        {
            if (item.CriterionGuid != Guid.Empty)
            {
                return Update(item);
            }
            else
            {
                return Create(item);
            }
        }

        /// <summary>
        /// Deletes an instance of CriterionForGroupCustomer. Returns true on success.
        /// </summary>
        /// <param name="criterionGuid"> criterionGuid </param>
        /// <returns>bool</returns>
        public bool Delete(Guid CriterionGuid)
        {
            return CriterionCustomerDAL.Delete(CriterionGuid);
        }

        /// <summary>
        /// Gets an IList with all instances of CriterionForGroupCustomer.
        /// </summary>
        public List<CriterionCustomer> GetAll()
        {
            LoadDataForGroup(ConstantVariable.VALUE_EMTY_GUID);
            return ListResult;
        }

        /// <summary>
        /// Get one criterionCustomer by item guid
        /// </summary>
        /// <param name="criterionGuid"></param>
        /// <returns></returns>
        public CriterionCustomer GetOne(Guid criterionGuid)
        {
            using (IDataReader reader = CriterionCustomerDAL.GetOne(criterionGuid))
            {
                return PopulateFromReader(reader);
            }
        }

        public List<TowTypeParameters<Guid, string>> GetGroupLevelCustomers(string defaulText)
        {
            List<TowTypeParameters<Guid, string>> Groups = new List<TowTypeParameters<Guid, string>>();
            using (IDataReader reader = CriterionCustomerDAL.GetAllGroup(ConstantVariable.VALUE_EMTY_GUID))
            {
                while (reader.Read())
                {
                    TowTypeParameters<Guid, string> item = new TowTypeParameters<Guid, string>();
                    item.T = new Guid(reader[ConstantVariable.Column_CriterionGuid].ToString());
                    item.V = reader[ConstantVariable.Column_CriterionName].ToString();
                    Groups.Add(item);
                }
            }
            Groups.Insert(0, new TowTypeParameters<Guid, string>(Guid.Empty, defaulText));
            return Groups;
        }
        #endregion
    }
}
